/**
 * 
 */
package util;

import static org.junit.Assert.fail;

import java.io.File;
import java.io.IOException;

import org.apache.commons.io.FileUtils;
import org.junit.Assert;
import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.interactions.Action;
import org.openqa.selenium.interactions.Actions;
import org.openqa.selenium.support.ui.Select;

import basetest.BaseTest;

/**
 * @author rnaidu webdriver utils
 * 
 */
public class SeleniumUtils extends BaseTest{
	
	// navigates to a URL
	public void navigate(String URL) {
		try {
			System.out.println("Naviating to " + URL);
			driver.get(CONFIG.getProperty(URL));
		} catch (Exception e) {
			SeleniumUtils.takeScreenShot();
			e.printStackTrace();
			
		}
	}
		
		public String geturl(String url) {
			try {
				System.out.println("geting " + url);
				driver.get(CONFIG.getProperty(url));
			} catch (Exception e) {
				SeleniumUtils.takeScreenShot();
				e.printStackTrace();
				
			}
			return url;
		}
	

	// clearby ID

	public void clearBYID(String objectName) {

		driver.findElement(By.id(OR.getProperty(objectName))).clear();
	}
	
	public void clearCss(String objectName) {

		driver.findElement(By.cssSelector(OR.getProperty(objectName))).clear();
	}

	// clicking on any object
	public void clickByXPath(String objectName) {

		driver.findElement(By.xpath(OR.getProperty(objectName))).click();
		
	}

	public void clickByCSS(String objectName) {

		driver.findElement(By.cssSelector(OR.getProperty(objectName))).click();
	}

	public void clickBYlinkText(String text) {
		driver.findElement(By.linkText(text)).click();
	}

	public void clickById(String objectName) {

		driver.findElement(By.id(OR.getProperty(objectName))).click();
	}

	public void clickByClass(String objectName) {

		driver.findElement(By.className(OR.getProperty(objectName))).click();
	}

	public void clickByName(String objectName) {

		driver.findElement(By.name(OR.getProperty(objectName))).click();
	}

	// Type

	public void typeByXpath(String text, String objectName) {

		driver.findElement(By.xpath(OR.getProperty(objectName))).sendKeys(text);
	}

	public void typeById(String text, String objectName) {

		driver.findElement(By.id(OR.getProperty(objectName))).sendKeys(text);
	}

	public void typeByName(String text, String objectName) {

		driver.findElement(By.name(OR.getProperty(objectName))).sendKeys(text);
	}

	public void typeByClass(String text, String objectName) {

		driver.findElement(By.className(OR.getProperty(objectName))).sendKeys(
				text);
	}

	public void typeByCSS(String text, String objectName) {

		driver.findElement(By.cssSelector(OR.getProperty(objectName)))
				.sendKeys(text);
	}

	// Select
	public void selectByid_visibletext(String text, String objectName) {

		Select te = new Select(driver.findElement(By.id((OR
				.getProperty(objectName)))));
		te.selectByVisibleText(text);

	}

	public void selectByclass_visibletext(String text, String objectName) {

		Select te = new Select(driver.findElement(By.className((OR
				.getProperty(objectName)))));
		te.selectByVisibleText(text);

	}

	public void selectByName_visibletext(String text, String objectName) {

		Select te = new Select(driver.findElement(By.name((OR
				.getProperty(objectName)))));
		te.selectByVisibleText(text);

	}

	public void selectByCSS_visibletext(String text, String objectName) {

		Select te = new Select(driver.findElement(By.cssSelector((OR
				.getProperty(objectName)))));
		te.selectByVisibleText(text);

	}

	public void selectByxpath_visibletext(String text, String objectName) {

		Select te = new Select(driver.findElement(By.xpath((OR
				.getProperty(objectName)))));
		te.selectByVisibleText(text);

	}

	public void selectByid_ByIndex(int text, String objectName) {

		Select te = new Select(driver.findElement(By.id((OR
				.getProperty(objectName)))));
		te.selectByIndex(text);

	}

	public void selectByclass_ByIndex(int text, String objectName) {

		Select te = new Select(driver.findElement(By.className((OR
				.getProperty(objectName)))));
		te.selectByIndex(text);

	}

	public void selectByName_ByIndex(int text, String objectName) {

		Select te = new Select(driver.findElement(By.name((OR
				.getProperty(objectName)))));
		te.selectByIndex(text);

	}

	public void selectByCSS_ByIndex(int text, String objectName) {

		Select te = new Select(driver.findElement(By.cssSelector((OR
				.getProperty(objectName)))));
		te.selectByIndex(text);

	}

	public void selectByxpath_ByIndex(int text, String objectName) {

		Select te = new Select(driver.findElement(By.xpath((OR
				.getProperty(objectName)))));
		te.selectByIndex(text);

	}

	public void selectByid_ByValue(String text, String objectName) {

		Select te = new Select(driver.findElement(By.id((OR
				.getProperty(objectName)))));
		te.selectByValue(text);

	}

	public void selectByclass_ByValue(String text, String objectName) {

		Select te = new Select(driver.findElement(By.className((OR
				.getProperty(objectName)))));
		te.selectByValue(text);

	}

	public void selectByName_ByValue(String text, String objectName) {

		Select te = new Select(driver.findElement(By.name((OR
				.getProperty(objectName)))));
		te.selectByValue(text);

	}

	public void selectByCSS_ByValue(String text, String objectName) {

		Select te = new Select(driver.findElement(By.cssSelector((OR
				.getProperty(objectName)))));
		te.selectByValue(text);

	}

	public void selectByxpath_ByValue(String text, String objectName) {

		Select te = new Select(driver.findElement(By.xpath((OR
				.getProperty(objectName)))));
		te.selectByValue(text);

	}

	
	// text present

	public boolean istextdisplayedById(String Expected, String objectName) {
		String Actual = driver.findElement(By.id((OR.getProperty(objectName))))
				.getText();

		Assert.assertEquals(Expected, Actual);
		return false;

	}

	public boolean istextdisplayedbyCSS(String Expected, String objectName) {
		String Actual = driver.findElement(
				By.cssSelector((OR.getProperty(objectName)))).getText();

		Assert.assertEquals(Expected, Actual);
		return false;
	}

	public boolean ismultipletextdisplayedbyName(String objectName,
			String Expected) {
		String Actual = driver.findElement(
				By.name((OR.getProperty(objectName)))).getText();

		Assert.assertEquals(Expected, Actual);
		return false;
	}
	public void ismultipletextdisplayedbycss(String objectName,String Expected) {
		String Actual = driver.findElement(
				By.cssSelector((OR.getProperty(objectName)))).getText();
	

		Assert.assertEquals(Expected, Actual);
	}
	public void ismultipletextdisplayedbyXpath(String objectName,String Expected) {
		String Actual = driver.findElement(
				By.xpath((OR.getProperty(objectName)))).getText();
	

		Assert.assertEquals(Expected, Actual);
	}

	public boolean istextdisplayedbyxpath(String Expected, String objectName) {
		String Actual = driver.findElement(
				By.xpath((OR.getProperty(objectName)))).getText();
		System.out.println(Actual);

		Assert.assertEquals(Expected, Actual);
		return false;
	}

	public void istextdisplayedbyclass(String Expected, String objectName) {
		String Actual = driver.findElement(
				By.className((OR.getProperty(objectName)))).getText();

		Assert.assertEquals(Expected, Actual);
	}
	
	//text not present
	public boolean textnotdisplayedById(String Expected, String objectName) {
		String Actual = driver.findElement(By.id((OR.getProperty(objectName))))
				.getText();

		//Assert.assertNotSame(Expected, Actual);
		Assert.assertNotSame(Actual, Expected);
		return false;

	}

	// text By attribute

	public void istextdisplayedByAttributename(String Expected,
			String objectName) {
		String Actual = driver.findElement(By.id((OR.getProperty(objectName))))
				.getAttribute("name");

		Assert.assertEquals(Expected, Actual);
	}

	public void istextdisplayedByattributevalue(String Expected,
			String objectName) {
		String Actual = driver.findElement(By.id((OR.getProperty(objectName))))
				.getAttribute("value");

		Assert.assertEquals(Expected, Actual);
	}

	public void iselementdisplayedByattribute_classname(String Expected,
			String objectName) {
		String Actual = driver.findElement(
				By.className((OR.getProperty(objectName)))).getAttribute(
				"checked");

		Assert.assertEquals(Expected, Actual);
	}

	// radio button selected
	public boolean radiobuttonSelectedByXpath(String objectName) {
		return driver.findElement(By.xpath(OR.getProperty(objectName)))
				.isSelected();
	}

	public boolean radiobuttonSelectedById(String objectName) {
		return driver.findElement(By.id(OR.getProperty(objectName)))
				.isSelected();
	}

	public boolean radiobuttonSelectedByclass(String objectName) {
		return driver.findElement(By.className(OR.getProperty(objectName)))
				.isSelected();
	}

	public boolean radiobuttonSelectedBYName(String objectName) {
		return driver.findElement(By.name(OR.getProperty(objectName)))
				.isSelected();
	}

	public boolean radiobuttonSelectedBycss(String objectName) {
		return driver.findElement(By.cssSelector(OR.getProperty(objectName)))
				.isSelected();
	}

	// javascript

	public void executeJavascript(String javascriptExpression) {
		JavascriptExecutor js = (JavascriptExecutor) driver;
		js.executeScript(javascriptExpression);
	}

	public Object getObjectFromJavascript(String javascriptExpression) {

		JavascriptExecutor js = (JavascriptExecutor) driver;

		try {
			Object o = js.executeScript("return " + javascriptExpression);
			log.debug("Javascript: " + javascriptExpression + " gives "
					+ (o == null ? null : o.toString()));
			return o;
		} catch (Exception e) {
			throw new RuntimeException("Unable to execute javascript: "
					+ javascriptExpression, e);
		}
	}

	public String getStringFromJavascript(String javascriptExpression) {
		return (String) getObjectFromJavascript(javascriptExpression);
	}

	public boolean getBooleanFromJavascript(String javascriptExpression) {
		return (Boolean) getObjectFromJavascript(javascriptExpression);
	}

	public long getLongFromJavascript(String javascriptExpression) {
		Object o = getObjectFromJavascript(javascriptExpression);
		if (o instanceof Long)
			return (Long) o;
		return Long.parseLong((String) o);
	}

	public double getDoubleFromJavascript(String javascriptExpression) {
		return (Double) getObjectFromJavascript(javascriptExpression);
	}
	
	public void assertNoJavascriptErrorsOccurred() {
		long l = getLongFromJavascript("javascriptErrors.length");

		if (l == 0)
			return;
		String s = getStringFromJavascript("javascriptErrors[0]");

		fail(l + " javascript errors.  First error is " + s);
	}
	
	
	// upload file

	public void uploadfileByname(String filePath) {
		// filePath = "path\\to\\file\for\\upload";
		JavascriptExecutor jsx = (JavascriptExecutor) driver;
		jsx.executeScript("document.getElementByname('data').value='"
				+ filePath + "';");
		if(true){
			throw new IllegalArgumentException("Error in uploadfileName method");
		}
	}

	// is element present

	public boolean isElementPresentByXpath(String objectName) {

		int count = driver.findElements(By.xpath(OR.getProperty(objectName)))
				.size();
		if (count == 0)
			return false;
		else
			return true;
	}

	public boolean isElementPresentBycss(String objectName) {

		int count = driver.findElements(
				By.cssSelector(OR.getProperty(objectName))).size();
		if (count == 0)
			return false;
		else
			return true;
	}

	public boolean isElementPresentByclass(String objectName) {

		int count = driver.findElements(
				By.className(OR.getProperty(objectName))).size();
		if (count == 0)
			return false;
		else
			return true;
	}

	public boolean isElementPresentByid(String objectName) {

		int count = driver.findElements(By.id(OR.getProperty(objectName)))
				.size();
		if (count == 0)
			return false;
		else
			return true;
	}
	public boolean VerifyPresentBylinttext(String text) {
		
		boolean name = driver.findElement(By.linkText(text)).isDisplayed();
		return name;
	}

	// verify page title

	public boolean VerifyPageTitle(String ExpectedPageTitle) {
		try {
			String ActualPageTitle = driver.getTitle();
			Assert.assertEquals(ExpectedPageTitle, ActualPageTitle);
		} catch (Exception e) {
			SeleniumUtils.takeScreenShot();
			e.printStackTrace();
		}
		
		return false;
	}

	// switch window

	public void SwicthwindowByID(String franename, String objectName) {
		WebElement Element = driver.findElement(By.id(OR
				.getProperty(objectName)));
		driver.switchTo().frame(franename);

	}

	// Refresh

	public void refreshpage() {
		driver.navigate().refresh();
	}

	// Navigate page back
	public void navigatepageback() {
		driver.navigate().back();

	}

	// Navigate page forward
	public void navigatepageforward() {
		driver.navigate().forward();

	}
	//is element visible
	public void iselementvisibleByid(String objectName){
		WebElement element  = driver.findElement(By.id(OR.getProperty(objectName)));
		if(element instanceof WebElement) {
		System.out.println("Element visible");
		} else {
		System.out.println("Element Not visible");
		}
		
	}
	// ScreenShot
	public void takeScreenShot()  
    {  
         try {  
        	 TakesScreenshot takesScreenshot = (TakesScreenshot) driver;
             File file = takesScreenshot.getScreenshotAs(OutputType.FILE);
            String name = "bug";
         
             File destFile = new File("target/cucumber/screenshot/" + name + ".png");
             try {
                 FileUtils.copyFile(file, destFile);
             } catch (IOException e) {
                 e.printStackTrace();
             }
         }   
         catch (Exception e) {  
              e.printStackTrace();  
         }  
    }  
                   //close
			public void close() {
				driver.close();
				driver.quit();

			}
			
			//actions
			
			public void dragElement(WebElement webElement, int xOffSet, int yOffSet) {

				Actions builder = new Actions(driver);
				Action dragAndRelease = builder.clickAndHold(webElement)
						.moveByOffset(xOffSet, yOffSet).release().build();
				dragAndRelease.perform();
			    
			}

			
//record
			public void zoomMapUsingDoubleClick(WebElement e) {
				Actions builder = new Actions(driver);
				builder.doubleClick(e).build().perform();
				
			}
			
			
}